﻿/**
 * Create in 2012-4-4.
 */
package kit4j.common.task.executor;

import kit4j.common.task.Task;

/**
 * 顺序的任务执行器。所有提交到该任务执行器的任务都将按提交的先后顺序依次执行。
 * 
 * @author MiniKnife
 * 
 */
public class SequentialTaskExecutor extends AbstractTaskExecutor {

	private Task currentTask;

	private Object currentTaskLock;

	public SequentialTaskExecutor() {
		currentTaskLock = new Object();
	}

	/**
	 * @see kit4j.common.task.executor.AbstractTaskExecutor#runTask(kit4j.common.task.Task)
	 */
	@Override
	protected void runTask(Task task) {
		synchronized (currentTaskLock) {
			currentTask = task;
		}
		
		task.start();
	}

	/**
	 * @see kit4j.common.task.executor.AbstractTaskExecutor#pause()
	 */
	@Override
	public void pause() {
		synchronized (currentTaskLock) {
			currentTask.pause();
		}
	}

	/**
	 * @see kit4j.common.task.executor.AbstractTaskExecutor#resume()
	 */
	@Override
	public void resume() {
		synchronized (currentTaskLock) {
			currentTask.resume();
		}
	}

	/**
	 * @see kit4j.common.task.executor.AbstractTaskExecutor#stop()
	 */
	@Override
	public void stop() {
		synchronized (currentTaskLock) {
			currentTask.stop();
		}
	}

}
